package com.haiyou.common.util.rest;

import java.util.Calendar;
import java.util.Date;

import com.haiyou.common.constants.game.GameConstants;
import com.haiyou.common.util.time.TimeUtil;


public class ResetTimeUtil {
	/** 累计天数*/
	private static int countDay;
	/** 每日重置时间*/
	private static Date resetTime;
	/** 下一日重置时间*/
	private static Date nextResetTime;
	/** 每周重置时间*/
	private static Date resetWeekTime;
	/** 每周下一次重置时间*/
	private static Date nextResetWeekTime;
	/** 每月重置时间*/
	private static Date resetMonthTime;
	/** 每月下一次重置时间*/
	private static Date nextResetMonthTime;
	
	/** 招募阵容最大数量 */
	public static int RECURIT_CAMP_MAX = 5;
	
	private static void resetResetTime(){
		if(nextResetTime == null || nextResetTime.getTime() <= System.currentTimeMillis()){
			synchronized (TimeUtil.class) {
				if(nextResetTime == null || nextResetTime.getTime() <= System.currentTimeMillis()){
					resetTime = TimeUtil.getDate(GameConstants.RESET_HOUR4, 0, 0);
					if(resetTime.getTime() > System.currentTimeMillis()){
						resetTime = TimeUtil.getNextDate(resetTime, Calendar.DAY_OF_YEAR, -1);//如果时间大于当前，回滚一天
					}
					nextResetTime = TimeUtil.getNextDate(resetTime, Calendar.DAY_OF_YEAR, 1);
					if(System.currentTimeMillis() < resetTime.getTime()){
						countDay = TimeUtil.getCountDay(new Date(resetTime.getTime() - TimeUtil.HOUR * (GameConstants.RESET_HOUR4 + 1)));
					}else{
						countDay = TimeUtil.getCountDay(resetTime);
					}
				}
			}
		}
	}
	public static long getResetTime(){
		resetResetTime();
		return resetTime.getTime();
	}
	public static Date getResetDate(){
		return new Date(getResetTime());
	}
	public static long getNextResetTime(){
		resetResetTime();
		return nextResetTime.getTime();
	}
	public static Date getNextResetDate(){
		return new Date(getNextResetTime());
	}
	public static int getCountDay(){
		resetResetTime();
		return countDay;
	}
	
	private static void resetResetWeekTime(){
		if(nextResetWeekTime == null || nextResetWeekTime.getTime() <= System.currentTimeMillis()){
			synchronized (TimeUtil.class) {
				if(nextResetWeekTime == null || nextResetWeekTime.getTime() <= System.currentTimeMillis()){
					resetWeekTime = TimeUtil.getDateByWeekDay(1, GameConstants.RESET_HOUR4, 0, 0);
					if(resetWeekTime.getTime() > System.currentTimeMillis()){
						resetWeekTime = TimeUtil.getNextDate(resetWeekTime, Calendar.WEEK_OF_YEAR, -1);//如果时间大于当前，回滚一周
					}
					nextResetWeekTime = TimeUtil.getNextDate(resetWeekTime, Calendar.WEEK_OF_YEAR, 1);
				}
			}
		}
	}
	
	public static long getResetWeekTime(){
		resetResetWeekTime();
		return resetWeekTime.getTime();
	}
	
	public static long getNextResetWeekTime(){
		resetResetWeekTime();
		return nextResetWeekTime.getTime();
	}
	
	private static void resetResetMonthTime(){
		if(nextResetMonthTime == null || nextResetMonthTime.getTime() <= System.currentTimeMillis()){
			synchronized (TimeUtil.class) {
				if(nextResetMonthTime == null || nextResetMonthTime.getTime() <= System.currentTimeMillis()){
					resetMonthTime = TimeUtil.getMonthDayDate(1, GameConstants.RESET_HOUR4, 0, 0);
					if(resetMonthTime.getTime() > System.currentTimeMillis()){
						resetMonthTime = TimeUtil.getNextDate(resetMonthTime, Calendar.MONTH, -1);//如果时间大于当前，回滚一个月
					}
					nextResetMonthTime = TimeUtil.getNextDate(resetMonthTime, Calendar.MONTH, 1);
				}
			}
		}
	}
	public static long getResetMonthTime(){
		resetResetMonthTime();
		return resetMonthTime.getTime();
	}
	public static long getNextResetMonthTime(){
		resetResetMonthTime();
		return nextResetMonthTime.getTime();
	}
	
	/**
	 * 廖权辉要求全服统一招募阵营
	 * @return
	 */
	public static byte getRecruitCamp(){
		return (byte) ((getCountDay() % RECURIT_CAMP_MAX) + 1);
	}
	
	/**
	 * 是否今天(以凌晨4点为重置点)
	 */
	public static boolean isToday(Date time){
		if(time == null){
			return false;
		}
		resetResetTime();
		return resetTime.getTime() <= time.getTime() && time.getTime() < nextResetTime.getTime();
	}
	
	/**
	 * 获取间隔天数(以凌晨4点为重置点)
	 */
	public static int getIntervalDay(Date time){
		if(time == null){
			return 0;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		if(calendar.get(Calendar.HOUR_OF_DAY) < GameConstants.RESET_HOUR4){
			calendar.add(Calendar.DATE, -1);  
		}
		calendar.set(Calendar.HOUR_OF_DAY, GameConstants.RESET_HOUR4);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return TimeUtil.getIntervalDay(calendar.getTimeInMillis(), getResetTime());
	}
	
	/**
	 * 获取间隔周数(以凌晨4点为重置点)
	 */
	public static int getIntervalWeek(Date time){
		if(time == null){
			return 0;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		if(calendar.get(Calendar.HOUR_OF_DAY) < GameConstants.RESET_HOUR4){
			calendar.add(Calendar.DATE, -1);  
		}
		calendar.set(Calendar.HOUR_OF_DAY, GameConstants.RESET_HOUR4);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return TimeUtil.getIntervalWeek(calendar.getTimeInMillis(), getResetTime());
	}
	
	/**
	 * 获取当周周一间隔周数(以凌晨4点为重置点)
	 */
	public static int getIntervalWeekByMonday(Date time){
		if(time == null){
			return 0;
		}
		Date oldMonday = getDateByWeekByMonday(time);
		Date curMonday = getDateByWeekByMonday(null);
		return TimeUtil.getIntervalWeek(oldMonday, curMonday);
	}
	
	private static Date getDateByWeekByMonday(Date time){
		Calendar calendar = Calendar.getInstance();
		if(time != null){
			calendar.setTime(time);
		}
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		day = day == 1 ? 7 : day - 1;
		if(day != 1){
			calendar.add(Calendar.DATE, -(day - 1));
		}else if(calendar.get(Calendar.HOUR_OF_DAY) < GameConstants.RESET_HOUR4){
			calendar.add(Calendar.DATE, -1);  
		}
		calendar.set(Calendar.HOUR_OF_DAY, GameConstants.RESET_HOUR4);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 获取某个时间N天后
	 * @param time
	 * @param day
	 * @return
	 */
	public static Date getNextTimeByDay(Date time, int day){
		if(time == null){
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		if(calendar.get(Calendar.HOUR_OF_DAY) < GameConstants.RESET_HOUR4){
			calendar.add(Calendar.DATE, -1);  
		}
		calendar.set(Calendar.HOUR_OF_DAY, GameConstants.RESET_HOUR4);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.add(Calendar.DAY_OF_YEAR, day);
		return calendar.getTime();
	}
	
	/**
	 * 获取当前第几天
	 * @param time
	 * @return
	 */
	public static int getCountDay(Date time){
		return getIntervalDay(time) + 1;
	}
	
	public static int getCountDay(long time){

		return getCountDay(new Date(time));
	}
	/**
	 * 获取当前第几周
	 */
	public static int getCountWeek(Date time){
		return getIntervalWeekByMonday(time) + 1;
	}
	
	public static Date getResetTime(Date time){
		if(time == null){
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		if(calendar.get(Calendar.HOUR_OF_DAY) < GameConstants.RESET_HOUR4){
			calendar.add(Calendar.DATE, -1);  
		}
		calendar.set(Calendar.HOUR_OF_DAY, GameConstants.RESET_HOUR4);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
}
